/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.rpc;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.attach.pojo.param.AttachUpdateParam;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import com.tarena.lbs.attach.pojo.vo.FileVO;
import com.tarena.lbs.attach.service.AttachService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

@DubboService
@Slf4j
public  class AttachRpcService implements AttachApi {


    @Override
    public List<AttachDTO> batchGetAttachInfo(List<Integer> ids, Integer type) {
        return Collections.emptyList();
    }

    @Override
    public int batchUpdateAttachByIdList(List<AttachUpdateParam> attachUpdateParamList) {
        return 0;
    }

    @Override
    public int deleteAttachByBusinessIdAndBusinessType(AttachUpdateParam attachUpdateParam) {
        return 0;
    }

    @Override
    public int deleteAttachById(Long id) {
        return 0;
    }

    @Override
    public int deleteAttachInfoByParam(AttachQuery attachQuery) {
        return 0;
    }

    @Override
    public List<AttachDTO> getAttachInfo(AttachQuery attachQuery) {
                return null;
    }

    @Override
    public boolean batchUpdateBusiness(List<PicUpdateParam> picParams) {
        return false;
    }

    @Override
    public AttachQrDTO generateQrCode(AttachQRParam attachQRParam) {
        return null;
    }

    @Autowired
    private AttachService attachService;


    @Override
    public AttachQrDTO saveAttachNo(byte[] fileBytes, String originalFilename) throws Exception {
        // 参数校验
        if (fileBytes == null || fileBytes.length == 0) {
            throw new IllegalArgumentException("文件字节数组不能为空");
        }
        if (originalFilename == null || originalFilename.trim().isEmpty()) {
            throw new IllegalArgumentException("原始文件名不能为空");
        }

        // 日志记录（假设已集成日志框架）
        // log.info("开始保存附件，文件名：{}", originalFilename);

        // 将 byte[] 转换为 MultipartFile
        InputStream is = new ByteArrayInputStream(fileBytes);
        MultipartFile multipartFile = new CustomMultipartFile(is, originalFilename);

        // 直接调用 Service 层
        FileVO fileVO = attachService.uploadFile(multipartFile);
        log.debug("文件上传={}", fileVO);

        AttachQrDTO attachQrDTO = new AttachQrDTO();
        BeanUtils.copyProperties(fileVO,  attachQrDTO);
        log.debug("复制结果={}",attachQrDTO);
        return attachQrDTO;

    }
    // 自定义 MultipartFile 实现类（需自行实现）
    private static class CustomMultipartFile implements MultipartFile {
        private final InputStream inputStream;
        private final String filename;

        public CustomMultipartFile(InputStream inputStream, String filename) {
            this.inputStream = inputStream;
            this.filename = filename;
        }

        @Override
        public String getName() {
            return "file";
        }

        @Override
        public String getOriginalFilename() {
            return filename;
        }

        @Override
        public String getContentType() {
            return "application/octet-stream";
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public long getSize() {
            try {
                return inputStream.available();
            } catch (IOException e) {
                return 0;
            }
        }

        @Override
        public byte[] getBytes() throws IOException {
            return new byte[0];
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return inputStream;
        }

        @Override
        public void transferTo(java.io.File dest) throws IOException, IllegalStateException {
            try (InputStream is = inputStream) {
                java.nio.file.Files.copy(is, dest.toPath());
            }
        }
    }


}